<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>统计规则设置</title>
    <link rel="stylesheet" type="text/css" href="../lib/laydate.css"/>
    <link rel="stylesheet" type="text/css" href="../lib/pagination/style/pagination.css"/>
    <link rel="stylesheet" type="text/css" href="../css/base.css"/>
    <script type="text/javascript" src="../js/common/language.js" ></script>
    <script type="text/javascript" src="../js/news/jquery-1.9.1.js"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.ui.widget.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.iframe-transport.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.fileupload.js" type="text/javascript" charset="utf-8"></script>
    <script src="../js/news/page.js"></script>
    <script src="/lib/layer/layer.js?20201106"></script>
    <script src="/js/base/base.js"></script>
    <script src="/lib/laydate/laydate.js"></script>
    <script src="/js/ajaxupload.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jquery.form.min.js"></script>
    <script src="../lib/pagination/js/jquery.pagination.min.js" type="text/javascript" charset="utf-8"></script>
    <%--<script src="../lib/layer/layer.js?20201106"></script>--%>
    <style>
        a{
            text-decoration: none;
            color: #207bd6;
        }

        .buttonDiv>div{
            float: left;
            margin: 20px 10px;
        }
        .fileDone select{
            width: 160px;
            height:28px;
        }
        #tr_td tr:nth-child(odd){
            background-color: #fff;
        }
        input{
            float: none;
        }
        .newMange input[type="text"]{
            width: 260px;
            height: 30px;
        }
        select{
            width: 260px;
            height: 30px;
        }
        textarea{
            width: 260px;
            height: 50px;
            vertical-align: middle;
        }
        a{
            text-decoration: none;
            color: #207bd6;
        }
        .newTbale tr td{
            border-right: #ccc 1px solid;
            padding: 5px;
        }
        .divTable{
            width: 80%;
            margin: 0px auto;
        }
        .divTable table{
            width: 100%;
        }
        .divTable table tr th{
            padding: 8px;
            font-size: 13pt;
            color: #2F5C8F;
        }
        .divTable table tr td{
            text-align: center;
        }
        .divBtn{
            float: right !important;
            width: 103px;
            height: 28px;
            background: #2b7fe0;
            color: #fff;
            font-size: 14px;
            line-height: 28px;
            margin-right: 4%;
            margin-top: 20px;
            cursor: pointer;
            border-radius: 4px;
        }

        .return{
            width: 103px;
            height: 28px;
            background: #2b7fe0;
            border-radius: 4px;
            cursor: pointer;
            text-align: center;
            color: #ffffff;
            line-height: 28px;
            float: right;
            font-size: 14px;
            margin: 20px 70px 0 0;
        }
        .return a{
            color: #ffffff;
            padding:0 30px;
        }
        .ruleForm{
            width:60%;
            margin: 0 auto;
            display: none;
        }
        .ruleForm .ruleTextarea{
            width: 400px;
            height: 80px;
            border-radius: 5px;
            vertical-align: middle;
            background-color: rgb(235, 235, 228);
        }
        .ruleForm select{
            width: 60px;
            margin: 5px;
            border-radius: 4px;
        }
        .ruleForm .ruleAddBtn{
            width: 103px;
            height: 40px;
            background: #2b7fe0;
            color: #fff;
            font-size: 14px;
            text-align: center;
            cursor: pointer;
            border-radius: 4px;
        }
        .preserve{
            width: 103px;
            height: 40px;
            background: #2b7fe0;
            color: #fff;
            font-size: 14px;
            text-align: center;
            cursor: pointer;
            border-radius: 4px;
        }


        .ruleForm .ruleNoBtn{
            width: 103px;
            height: 40px;
            background: #2b7fe0;
            color: #fff;
            font-size: 14px;
            text-align: center;
            cursor: pointer;
            border-radius: 4px;
        }
        #s1{
            width: 400px;
            height: 80px;
            border-radius: 5px;
            vertical-align: middle;
        }
    </style>
</head>

<body>
<div class="bx">

    <div class="navigation  clearfix juMange" id="hoSet" style="display: block;">
        <div class="left" style="margin-left: 30px">
            <img src="/img/commonTheme/${sessionScope.InterfaceModel}/icon_manageFile.png">
            <div class="news">统计规则设置</div>
        </div>
        <div class="return"><a href="/attendanceWay/attindex">返回</a></div>
        <div class="divBtn">
            <img src="../../img/mywork/newbuildworjk.png" alt="" style="margin-left: 6px;margin-top: -4px;">
            <span style="font-size: 11pt;">添加规则</span>
        </div>
        <div style="clear: both;"></div>
        <div class="divTable" style="display: block;">
            <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>部门</th>
                    <th>加班规则</th>
                    <th>迟到规则</th>
                    <th>旷工规则</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="trList">

                </tbody>
            </table>
            <%--<div class="right">--%>
                <%--<!-- 分页按钮-->--%>
                <%--<div class="M-box3" id="dbgz_page"></div>--%>
            <%--</div>--%>
        </div>


        <form class="ruleForm" action="" method="post" name="">
            <table class="" align="center">
                <tbody>
                <tr>
                    <td class="">适用部门：</td>
                    <td class="">
                        <input type="hidden" name="" value="">
                        <textarea name="dept" id="s1" disabled></textarea>
                        <a href="javascript:;" style="margin: 0 10px;" id="a1" class="a1">添加</a>
                        <a href="javascript:;" class="clearS1">清空</a>
                    </td>
                </tr>
                <tr>
                    <td nowrap="" class="">加班规则：</td>
                    <td nowrap="" class="">加班
                        <input style="width: 50px;border-radius: 4px; margin: 5px;" name="" id="overtimeHour" >

                        </input>小时,休
                        <input style="width: 50px;border-radius: 4px; margin: 5px;" name="" id="overtimeDay" >

                        </input>天
                    </td>
                </tr>
                <tr>
                    <td nowrap="" class="">迟到规则：</td>
                    <td nowrap="" class="">
                            <input type="radio" name="yes" class="lateType" value="0" checked="">
                        <label for="late_type0" style="display: inline-block">无 </label><br>
                            <input type="radio" name="yes" class="lateType" value="1">
                        <label for="late_type1" style="display: inline-block">每月允许迟到 </label>
                            <input type="text" style="width: 50px;border-radius: 4px; margin: 5px;" name="lateType" id="" class="lateOnce" >

                            </input>次,每次
                            <input style="width: 50px;border-radius: 4px; margin: 5px;" name="" id="" class="lateHour">

                            </input>分钟<br>
                            <input type="radio" name="yes" class="lateType" value="2">
                        <label for="late_type2" style="display: inline-block">每月允许迟到
                            <input style="width: 50px;border-radius: 4px; margin: 5px;" name="lateType" id="" class="lateOnce">

                            </input>次,迟到总时长不超过
                            <input style="width: 50px;border-radius: 4px; margin: 5px;" name="" id="" class="lateHour">

                            </input>分钟</label><br>
                            <input type="radio" name="yes" class="lateType" value="3">
                        <label for="late_type3" style="display: inline-block">每月每次允许迟到
                            <input style="width: 50px;border-radius: 4px; margin: 5px;" name="lateType" id="" class="lateOnce">

                            </input>分钟,迟到总时长不超过
                            <input style="width: 50px;border-radius: 4px; margin: 5px;" name="" id="" class="lateHour">

                            </input>分钟</label><br>
                            <input type="radio" name="yes" class="lateType" value="4">
                        <label for="late_type4" style="display: inline-block">每月允许累计迟到 </label>
                            <input style="width: 50px;border-radius: 4px; margin: 5px;" name="lateType" id="" class="lateOnce">

                            </input>分钟<br>
                    </td>
                </tr>
                <tr>
                    <td nowrap="" class="">旷工规则：</td>
                    <td nowrap="" class="">
                        单次迟到  <input style="width: 50px;border-radius: 4px; margin: 5px;" name="" id="oneLateHour" >

                    </input>分钟之上算旷工<br>
                        单次早退  <input style="width: 50px;border-radius: 4px; margin: 5px;" name="" id="oneEarlyHour" >

                    </input>分钟之上算旷工<br>
                    </td>
                </tr>
                <tr>
                    <td nowrap="" class="" colspan="2" style="text-align: center;">
                        <input type="button" value="添加统计规则" class="ruleAddBtn" onclick="">&nbsp;&nbsp;
                        <input type="button" value="保存" class="preserve" onclick="">&nbsp;&nbsp;
                        <input type="button" value="撤销" class="ruleNoBtn" onclick="">&nbsp;&nbsp;
                    </td>
                </tr>
                </tbody></table>
        </form>

    </div>
</div>
    <script type="text/javascript">
        $(".divbtn").click(function () {
            $(".divtable").hide();
            $(".return").hide();
            $(".divBtn").hide();
            $(".ruleForm").show();
            $(".ruleAddBtn").show();
            $(".preserve").hide();

        });
        $(".ruleNoBtn").click(function () {
            $(".divtable").show();
            $(".return").show();
            $(".divBtn").show();
            $(".ruleForm").hide();
            window.location.reload();

        });
        $(".clearS1").click(function () {
            $("#s1").attr('deptid','');
            $("#s1").val('');

        });


        //选择部门
        $('#a1').click(function(){
            dept_id="s1";
            $.popWindow("../../common/selectDept?allDept=1");
        });





        var overtimeHour;
        var overtimeDay;
        var lateType;
        var lateOnce;
        var lateHour;
        var oneLateHour;
        var oneEarlyHour;
        var preserveId;
        var yesOrNo;
        //添加统计规则
        $(".ruleAddBtn").click(function () {
             overtimeHour=$("#overtimeHour").val();
             overtimeDay=$("#overtimeDay").val();
             lateType=$(':radio[name="yes"]:checked').val();
             lateOnce=$(".lateOnce").eq(lateType-1).val();
             lateHour=$(".lateHour").eq(lateType-1).val();
             oneLateHour=$("#oneLateHour").val();
             oneEarlyHour=$("#oneEarlyHour").val();

            if ($("#s1").attr("deptid")==undefined){
                layer.msg('未选择部门，请重新选择！', {icon: 2,time:2000});
            }else if ($("#s1").attr("deptid")=="") {
                layer.msg('未选择部门，请重新选择！', {icon: 2, time: 2000});
            }else {
                $.ajax({
                    type:'post',
                    url:'/attendRule/insertAttendRule',
                    dataType:'json',
                    data:{
                        deptId:$("#s1").attr("deptid"),//部门ID
                        overtimeHour:overtimeHour,//加班小时数
                        overtimeDay:overtimeDay,//加班兑换天数
                        lateType:lateType,//迟到类型
                        lateOnce:lateOnce,//迟到次数
                        lateHour:lateHour,//迟到时间
                        oneLateHour:oneLateHour,//单次迟到为旷工',
                        oneEarlyHour:oneEarlyHour//单次早退为旷工',
                    },
                    success:function(res){
                        if(res.msg=="失败"){
//                            alert("适用部门："+res.object+"已存在，请重新选择！");
                            layer.msg("适用部门："+res.object+"已存在，请重新选择！", {icon: 2,time:2000});

                        }else {
//                            alert("添加成功！")
                            layer.msg('添加成功!', {icon: 1,time:2000});
                            setTimeout(function(){  window.location.reload(); }, 2000);
                        }
                    }
                })

            }






        })
        //查询统计规则
        $.ajax({
            type:'post',
            url:'/attendRule/getAttendRule',
            dataType:'json',
            success:function(res) {

               var data= res.obj;
               var resContent
                var iNum=0;
               $.each(data,function (i, item) {
                   iNum++;
                   var cdNum=item.lateType
                   resContent+='<tr class="" aId="'+item.arid+'">'+
                       '<td nowrap="" align="center">'+iNum+'</td>'+
                       '<td style="width: 20%;" align="center">'+function(){
                           if(item.deptName==undefined){
                               return '';
                           }else{
                               return item.deptName;
                           }
                           }()+'</td>'+
                       '<td align="center" >加班小时数:'+function(){
                           if(item.overtimeHour==undefined){
                               return ' ';
                           }else{
                               return item.overtimeHour;
                           }
                       }()+'小时<br>兑换天数:'+function(){
                           if(item.overtimeDay==undefined){
                               return ' ';
                           }else{
                               return item.overtimeDay;
                           }
                       }()+'天</td>'+
                       '<td align="center" >'+function(){
                           var lateOnce=0;
                           var lateHour=0;
                           if(item.lateOnce!=undefined){
                               lateOnce=item.lateOnce;
                           }
                           if(item.lateHour!=undefined){
                               lateHour=item.lateHour;
                           }
                           if(cdNum==0){
                               return '未定义迟到规则';
                           }else if(cdNum==1){
                               return '每月允许迟到:'+lateOnce+'次<br>每次:'+lateHour+'分钟';
                           }else if(cdNum==2){
                              return '每月允许迟到:'+lateOnce+'次<br>迟到总时长不超过:'+lateHour+'分钟';
                           }else if(cdNum==3){
                               return '每月每次允许迟到:'+lateOnce+'分钟<br>迟到总时长不超过:'+lateHour+'分钟';
                           }else if(cdNum==4){
                               return '每月允许累计迟到:'+lateOnce+'分钟';
                           }
                       }()+'</td>'+
                       '<td align="center" >单次迟到'+function(){
                           if(item.oneLateHour==undefined){
                               return '0';
                           }else{
                               return item.oneLateHour;
                           }
                       }()+'分钟为旷工<br>单次早退'+function(){
                           if(item.oneEarlyHour==undefined){
                               return '0';
                           }else{
                               return item.oneEarlyHour;
                           }
                       }()+'分钟为旷工</td>'+
                       '<td nowrap="" align="center">'+
                       '<a class="editGeneration"href="javascript:;"> 编辑</a>'+
                       '<a style="color: crimson" class="delGeneration" href="javascript:;"> 删除</a>'+
                       '</td>'+
                       '</tr>';

               })
                $("#trList").append(resContent);
                //    编辑统计规则
                $(".editGeneration").click(function () {
                    var fId=$(this).parent().parent().attr("aId");
                    preserveId=fId
                    $(".divtable").hide();
                    $(".return").hide();
                    $(".divBtn").hide();
                    $(".ruleForm").show();
                    $(".ruleAddBtn").hide();
                    $(".preserve").show();
                    $.ajax({
                        type:'post',
                        url:'/attendRule/getAttendRuleById',
                        dataType:'json',
                        data:{
                            id:preserveId
                        },
                        success:function(res){
                            var data=res.object;
                            $(':radio[name="yes"]').eq(data.lateType).attr("checked",'checked')
                            $("#oneLateHour").val(data.oneLateHour)
                            $("#oneEarlyHour").val(data.oneEarlyHour)
                            $(':radio[name="yes"]:checked').val(data.lateType)
                            $(".lateOnce").eq(data.lateType-1).val(data.lateOnce)
                            $(".lateHour").eq(data.lateType-1).val(data.lateHour)
                            $("#overtimeHour").val(data.overtimeHour)
                            $("#overtimeDay").val(data.overtimeDay)
                            $("#s1").val(data.deptName)
                            $("#s1").attr("deptid",data.deptId)
                        }
                    })
                })
                //  删除统计规则

                $(".delGeneration").click(function () {

                    var fId=$(this).parent().parent().attr("aId");

                    $(this).parent().parent().remove()

                    $.ajax({
                        type:'post',
                        url:'/attendRule/deleteAttendRule',
                        dataType:'json',
                        data:{
                            id:fId
                        },
                        success:function(res){
                            layer.msg('删除成功！', {icon: 2,time:2000});
                            setTimeout(function(){  window.location.reload(); }, 2000);

                        }
                    })
                });
            }
        })

        //    保存统计规则
        $(".preserve").click(function () {

            overtimeHour=$("#overtimeHour").val()
            overtimeDay=$("#overtimeDay").val()
            lateType=$(':radio[name="yes"]:checked').val()
            lateOnce=$(".lateOnce").eq(lateType-1).val()
            lateHour=$(".lateHour").eq(lateType-1).val()
            oneLateHour=$("#oneLateHour").val()
            oneEarlyHour=$("#oneEarlyHour").val()
            if($("#s1").val() == ""){
                $.layerMsg({content:'适用部门不能为空',icon:2});
                return false;
            }
            $.ajax({
                type:'post',
                url:'/attendRule/updateAttendRule',
                dataType:'json',
                data:{
                    arid:preserveId,
                    deptId:$("#s1").attr("deptid"),//部门ID
                    overtimeHour:overtimeHour,//加班小时数
                    overtimeDay:overtimeDay,//加班兑换天数
                    lateType:lateType,//迟到类型
                    lateOnce:lateOnce,//迟到次数
                    lateHour:lateHour,//迟到时间
                    oneLateHour:oneLateHour,//单次迟到为旷工',
                    oneEarlyHour:oneEarlyHour//单次早退为旷工',
                },
                success:function(res){
                    if(res.msg=="失败"){
//                        alert("适用部门："+res.object+"已存在，请重新选择！");
                        layer.msg("适用部门："+res.object+"已存在，请重新选择！", {icon: 2,time:2000});
                        console.log(res)
                    }else {
                        layer.msg('编辑保存成功！', {icon: 1,time:2000});
//                        alert("编辑保存成功！")


                        setTimeout(function()
                            {  window.location.reload(); $(".divtable").show();
                            $(".return").show();
                            $(".divBtn").show();
                            $(".ruleForm").hide() }, 2000);
                    }
                }
            })
        })
    </script>
</body>
</html>
